﻿// Decompiled with JetBrains decompiler
// Type: Netick.PacketAccumulator
// Assembly: Netick, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 6726ECA1-C773-4CF7-8952-E81B30D93B52
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.xml

using System;

#nullable disable
namespace Netick;

internal struct PacketAccumulator
{
  public float EffectiveRate;
  private int _accumulation;
  private float _accumulatedTime;
  private float _timeWindow;
  private float _expectedEffectiveRate;

  public float EstimatedLoss
  {
    get
    {
      return (double) Math.Abs(this.EffectiveRate - this._expectedEffectiveRate) < (double) this._expectedEffectiveRate * 0.02 ? 0.0f : Math.Clamp((float) (1.0 - (double) this.EffectiveRate / (double) this._expectedEffectiveRate), 0.0f, 1f);
    }
  }

  public PacketAccumulator(float effectiveRate, float timeWindow = 1f)
  {
    this._timeWindow = timeWindow;
    this._expectedEffectiveRate = effectiveRate;
    this.EffectiveRate = this._expectedEffectiveRate;
    this._accumulation = 0;
    this._accumulatedTime = 0.0f;
  }

  public void Add() => ++this._accumulation;

  public void Update(float delta)
  {
    this._accumulatedTime += delta;
    if ((double) this._accumulatedTime < (double) this._timeWindow)
      return;
    this.EffectiveRate = (float) this._accumulation;
    this._accumulation = 0;
    this._accumulatedTime = 0.0f;
  }

  public void Reset()
  {
    this.EffectiveRate = this._expectedEffectiveRate;
    this._accumulation = 0;
    this._accumulatedTime = 0.0f;
  }
}
